package com.lancoo.znbkxx.teacher.ui.adapter

import android.content.Context
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.ClassGrammarMasterDistribInfor
import kotlinx.android.synthetic.main.aibk_adapter_item_project_distribution.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_project_distribution_title.view.*
import java.math.BigDecimal

/*
* 学生语法掌握情况
* */
class AibkGrammarMasterStuIntervalAdapter(
        val context: Context?,
        val classGrammarMasterDistribInfor: ClassGrammarMasterDistribInfor
): KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View? {
        var view:View?=null
        if(position == 0){
            view = LayoutInflater.from(context)
                    .inflate(R.layout.aibk_adapter_item_project_distribution_title, parent, false)
        }else{
            view = LayoutInflater.from(context)
                    .inflate(R.layout.aibk_adapter_item_project_distribution, parent, false)
        }
        return view
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {
        if(position == 0){
            holder.convertView.TvProjectChangeX.setCompoundDrawables(null, null, null, null)
            holder.convertView.TvProjectX.text = "语法掌握率"
            holder.convertView.TvProjectChangeX.text = "语法掌握率较上周变化"
            holder.convertView.IvCheckArrowX.visibility = View.INVISIBLE
        }else{
            val stuName = classGrammarMasterDistribInfor.ClassTopicTable[position - 1].StuName
            val stuID = classGrammarMasterDistribInfor.ClassTopicTable[position - 1].StuID
            val curMasterRate = classGrammarMasterDistribInfor.ClassTopicTable[position - 1].CurMasterRate
            val masterRateChange = classGrammarMasterDistribInfor.ClassTopicTable[position - 1].MasterRateChange

            holder.convertView.TvStuName.text = stuName
            holder.convertView.TvStuID.text = stuID

            val bigDecimal1 = BigDecimal(curMasterRate)
            val dCurMasterRate = bigDecimal1.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()
            val strCurMasterRate = (dCurMasterRate*100).toInt().toString()+"%"
            holder.convertView.TvProject.text = strCurMasterRate

            val xMasterRateChange =  Math.abs(masterRateChange)
            val bigDecimal2 = BigDecimal(xMasterRateChange)
            val dMasterRateChange = bigDecimal2.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()
            val strMasterRateChange = LancooMethod.killlingDouble(dMasterRateChange*100)+"%"
            if(masterRateChange>0){
                holder.convertView.TvProjectChange.text= "+ "+ strMasterRateChange
            }else if(masterRateChange == 0.0){
                holder.convertView.TvProjectChange.text= "  "+ strMasterRateChange
            }else{
                holder.convertView.TvProjectChange.text= "- "+ strMasterRateChange
            }

            holder.convertView.TvStuName.setTextColor(context!!.resources.getColor(R.color.textColor2))
            holder.convertView.TvStuID.setTextColor(context!!.resources.getColor(R.color.textColor2))
            holder.convertView.TvProject.setTextColor(context!!.resources.getColor(R.color.textColor2))
            holder.convertView.TvProjectChange.setTextColor(context!!.resources.getColor(R.color.textColor2))

            var drawable: Drawable? = null
            if(masterRateChange>0) {
                drawable = context.resources.getDrawable(R.mipmap.aibk_icon_score_up)
                drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
            }else if(masterRateChange==0.0) {
                drawable = null
            }else{
                drawable = context.resources.getDrawable(R.mipmap.aibk_icon_score_down)
                drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
            }

            holder.convertView.TvProjectChange.setCompoundDrawables(null, null, drawable, null)
            holder.convertView.IvCheckArrow.visibility = View.VISIBLE
            holder.convertView.LlStuDiagnosisInforView.setOnClickListener {
                clickListener!!.onClick(position)
            }
        }
    }

    override fun getItemCount(): Int {
        val classScoreChangeSize = classGrammarMasterDistribInfor.ClassTopicTable.size
        return classScoreChangeSize+1
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    interface OnClickListener {
        fun onClick(pos: Int)
    }

    private var clickListener: OnClickListener? = null

    fun setClickListener(clickListener: OnClickListener?) {
        this.clickListener = clickListener
    }

}